package co.gov.medellin.ssti.sub.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import javax.naming.NamingException;

import co.gov.medellin.ssti.sub.common.ConexionFactory;
import co.gov.medellin.ssti.sub.model.dao.DAOFactory;
import co.gov.medellin.ssti.sub.model.dao.ISedeDAO;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.vo.SedeVO;
import co.gov.medellin.ssti.sub.model.vo.UsuarioxSedeVO;

public class SedeService {
	public List<SedeVO> loadAllSedes() throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.loadAllSedes();
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<UsuarioxSedeVO> findUsuarioxSede(UsuarioxSedeVO criterea)
			throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findUsuarioxSede(criterea);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<SedeVO> findSedes(String nombreSede) throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findSedes(nombreSede);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public String findNameByCode(String strCodeSede) throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findNameByCode(strCodeSede);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<SedeVO> findTotalSedebyUser(Long codeUser,
			Long codeSubpro, String groupUserName, Long areaUser)
			throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findTotalSedebyUser(codeUser, codeSubpro,
					groupUserName, areaUser);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<SedeVO> findTotalSedebyUser(Long codeUser,
			Long codeSubpro, String groupUserName, Long areaUser,
			String codeSede) throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findTotalSedebyUser(codeUser, codeSubpro,
					groupUserName, areaUser, codeSede);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<SedeVO> findSedesAsociadas(String codeSub)
			throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findSedesAsociadas(codeSub);
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}

	public Collection<SedeVO> findSedes() throws DAOException {
		ISedeDAO sedeDAO = null;
		try {
			Connection con = ConexionFactory.getInstance().getConnection();
			sedeDAO = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getSedeDAO(
					con);
			return sedeDAO.findSedes();
		} catch (NamingException e) {
			throw new DAOException(e);
		} catch (SQLException e) {
			throw new DAOException(e);
		} finally {
			if (sedeDAO != null) {
				if (!sedeDAO.isConnClosed()) {
					sedeDAO.close();
				}
			}
		}
	}
}
